package com.learn.eduorder.controller;

import com.learn.eduapi.entity.UserCourseOrder;
import com.learn.eduorder.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单控制器
 */
@RestController
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/order")
    public ResponseEntity<UserCourseOrder> makeOrder(@RequestBody UserCourseOrder order){
        orderService.saveOrder(order);
        return ResponseEntity.ok(order);
    }

    @PutMapping("/order")
    public ResponseEntity<UserCourseOrder> changeOrderStatus(@RequestBody UserCourseOrder order){
        UserCourseOrder userCourseOrder = orderService.changeOrderStatus(order.getTradeNo(), order.getStatus());
        return ResponseEntity.ok(userCourseOrder);
    }

    @GetMapping("/orders")
    public ResponseEntity<List<UserCourseOrder>> findUserOrders(long userId){
        List<UserCourseOrder> orders = orderService.findUserOrders(userId);
        return ResponseEntity.ok(orders);
    }

    @DeleteMapping("/order/{orderId}")
    public ResponseEntity<String> deleteOrder(@PathVariable("orderId") long orderId){
        orderService.deleteOrder(orderId);
        return ResponseEntity.ok("ok");
    }
}
